const query = require('../utils/DB')
class roleModel {
	// 获取角色列表
	static getRole() {
		return query(`select * from role`)
	}

	// 获取角色下的用户
	static getRoleUser(rid) {
		return query(`select * from admin where id in (select uid as id from adminrole where rid=${rid})`)
	}

	// 将某个用户 添加到 某个角色下下面
	static async addRoleUser(uid, rid) {
		await query(`delete from adminrole where uid=${uid}`)
		// console.log(`delete from adminrole where uid=${uid}`)
		return query(`insert into adminrole(rid,uid) values(${rid},${uid})`)
	}

	// 删除一个用户在角色中
	static delRoleUser(uid, rid) {
		return query(`delete from adminrole where uid=${uid} and rid=${rid}`)
	}

	// 添加角色
	static addRole(name, status) {
		return query(`INSERT INTO role(name, status) VALUES ('${name}', ${status})`)
	}

	// 修改角色
	static editRole(rid, name, status) {
		return query(`UPDATE role SET name = '${name}', status = ${status} WHERE rid =${rid}`)
	}

	// 删除角色
	static delRole(rid) {
		return query(`DELETE FROM role WHERE rid = ${rid}`)
	}
	
	static getUserRole(uid){
		return query(`select rid from adminrole where uid=${uid}`)
	}



}
module.exports = roleModel